package com.gy.zpchat;

import android.app.Activity;
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.bugly.crashreport.CrashReport;

import im.zego.zim.ZIM;
import com.gy.zpchat.util.PreferenceUtil;
import com.gy.zpchat.util.debug.log.EnContext;


public class MyApplication extends Application {

    public static Context context;
    public static String sUserId = "";
    public static boolean enableAppSign = true;
    private static Activity currentActivity;

    @Override
    public void onCreate() {
        context = this;
        super.onCreate();
        this.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);

        String token = PreferenceUtil.getInstance(this).getStringValue("token", "");
        if (!"".equals(token)) {
            KeyCenter.token = token;
        }
        String appID = PreferenceUtil.getInstance(this).getStringValue("app_id", "0");
        if (!"0".equals(appID)) {
            KeyCenter.appID = Long.parseLong(appID);
        }

        String appSign = PreferenceUtil.getInstance(this).getStringValue("app_sign", "0");
        if (!"0".equals(appSign)) {
            KeyCenter.appSign = appSign;
        }

        enableAppSign = PreferenceUtil.getInstance(this).getBooleanValue("enableAppSign", true);

        CrashReport.setUserId(ZIM.getVersion());
        CrashReport.initCrashReport(getApplicationContext(), "e039b18547", false);
        EnContext.setApplication(this);
        // 初始化 SDK
        SDKManager.sharedInstance().create(this, enableAppSign ? KeyCenter.appSign : null);

        initChannel();
    }


    private void initChannel(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (nm != null){
                NotificationChannel notificationChannel = new NotificationChannel("callChannel", "ZIM 呼叫邀请", NotificationManager.IMPORTANCE_HIGH);
                notificationChannel.setSound(Uri.parse("android.resource://"+getPackageName()+"/raw/call_sound_1"), null);
                nm.createNotificationChannel(notificationChannel);
            }
        }
    }

    private final ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacks() {

        @Override
        public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(@NonNull Activity activity) {

        }

        @Override
        public void onActivityResumed(@NonNull Activity activity) {
            currentActivity = activity;
        }

        @Override
        public void onActivityPaused(@NonNull Activity activity) {
            currentActivity = null;
        }

        @Override
        public void onActivityStopped(@NonNull Activity activity) {

        }

        @Override
        public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(@NonNull Activity activity) {

        }
    };

    public static Activity getCurrentActivity() {
        return currentActivity;
    }
}
